<?php
namespace app\index\controller;
use app\index\model\data;
use think\Controller;
use think\Request;
use think\Validate;


class Index extends Controller
{
    ///先拿到$ip
    public function __construct(Request $request = null)
    {
        $this->ip=request()->ip();

        parent::__construct($request);
    }

    //20用户端首页
    public function index($qy_id=null)
    {
        $ip=request()->ip();

//        var_dump($ip);
        $dc=new Data();

        $qy_name=$dc->d_qqy($qy_id);
        if(empty($qy_name)){
            $this->error('您所在的区域还未开通本系统服务，请联系“廊坊鸿星志金网络科技有限公司”');
        }
        session('ip',$ip);
        session('qy_id',$qy_id);
//        var_dump($qy_name);
        //已经通过审核的物资
        $new=$dc->d_qwuzi($qy_id,'str=2');
        //正在进行审核中的物资
        $wz_shz=$dc->d_qwuzi($qy_id,'str=1');
        $zjsum=$dc->d_qzjsum($qy_id);
        $wzsum=$dc->d_qwzsum($qy_id);
        if(empty($wz_shz)){
            $this->assign('wz_zsz',json_encode(0));
        }else{
            $this->assign('wz_zsz',json_encode(count($wz_shz)));
        }

        $this->assign('zjsum',json_encode($zjsum));
        $this->assign('wzsum',json_encode($wzsum));
        $this->assign('list',json_encode($new));

        $this->assign('qy_name',json_encode($qy_name));
        return $this->fetch();
    }
    //礼品网注册页面-自适应
    public function lp_zc($tgr_id=null){

        $this->assign('tgr_id',$tgr_id);

        return $this->fetch();
    }
    ///礼品网注册用户实体业务
    public function lp_zck($tgr_id=null){
        ///自动获取全局验证码post先验证验证码正确性
        $data['captcha']=input('post.code');
        $validate =new validate([
            'captcha'=>'captcha'
        ]);
        if(!$validate->check($data)){
            //验证失败
            $this->error($validate->getError());
        };
        $user['name']=$_POST['name'];
        ///检测输入的用户名是否重复
//         $new=db('lp_user')->where('name='.$_POST['name'])->find();
//         dump($new);
//         exit();
        $m_data=new Data();
        $is=$m_data->d_jc('lp_user','name',$user['name']);
        if($is){
            $this->error('用户名重复请重新输入一个');
        }
        $user['pass']=$_POST['pass'];
        $user['phone']=$_POST['phone'];
        $user['qq']=$_POST['qq'];
        $user['wx']=$_POST['wx'];
        $user['k_time']=time();
           if($tgr_id) {
               $user['tgr_id'] = $tgr_id;
           }
           $new=db('lp_user')->insert($user);
           if($new){
//               return "<script>
//                        alert('注册用户成功!');
//                        location.href='/';
//                        </script>";

               //               $this->result('注册用户成功！');


               $this->success('注册用户成功！','/');

           }else{
               $this->error('注册用户失败！');
           }

    }

    //20用户捐助页面
    public function wojuan(){
        return $this->fetch();
    }

    //20 捐助数据提交
    public function tjj(){
        $qy_id=session('qy_id');

        $m_data=new Data();
        $is=$m_data->d_wojuan($qy_id);
        if($is){
            return $this->success('提交捐助数据成功，审核通过后将显示信息','/index.php/index/?qy_id='.$qy_id);
        }else{
            return $this->error('提交捐助数据失败，请检查网络错误');
        }

    }


    //t3
    public function tisan(){
        return $this->fetch();
    }
    //t4
    public function tisi(){
        return $this->fetch();
    }
    //t5
    public function tiwu(){
        return $this->fetch();
    }
    //未到时间的页面竖屏（模板1）
    public function wtime(){

        return $this->fetch();
    }
    //未到时间的页面竖屏（模板2）
    public function wtime2(){

        return $this->fetch();
    }
    //未到开始终端使用状态
    public function wkai(){

        return $this->fetch();
    }
    //按钮触发后，改变当前状态为禁止！
    public function an_chufa(){
        $iszd=db('zhongduan')->where('ip',$this->ip)->find();
        $str=db('ks')->where('id',$iszd['ks_id'])->find();

//        switch ($str['str']){
//            case 1;
//                $new=db('ks')->where('id',$iszd['ks_id'])->update(['str'=>3]);
//                break;
//            case 2;
//                $new=db('ks')->where('id',$iszd['ks_id'])->update(['str'=>1]);
//                break;
//            case 3;
//                $new=db('ks')->where('id',$iszd['ks_id'])->update(['str'=>2]);
//                break;
//        }
        if($str['str']==1){
            $new=db('ks')->where('id',$iszd['ks_id'])->update(['str'=>3]);
        }else {
            $new = db('ks')->where('id',$iszd['ks_id'])->update(['str' => 1]);
        }
       if(empty($new))
       {
           echo -1;
       }else{
          echo 1;
       }

    }
}
